import java.util.ArrayList;

/**
 * 
 * @author Administrator Starting time 23:50
 * 
 */
public class Chessboard {
	// Add some attribute here
	ArrayList<Chess> chessList = new ArrayList<Chess>(9);
	StringBuffer locatedPositionsBF = new StringBuffer();
	IOHelper io = new IOHelper();

	public Chessboard() {
		for (int i = 0; i < 9; i++) {
			this.chessList.add(new Chess());
		}

	}

	public String toString() {
		StringBuffer str = new StringBuffer();
		str.append(' ');
		str.append(' ');
		str.append('A');
		str.append(' ');
		str.append('B');
		str.append(' ');
		str.append('C');
		str.append(System.getProperty("line.separator"));
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 4; j++) {
				if (j == 0) {
					str.append(i + 1);
					str.append(' ');
				} else {
					str.append(this.chessList.get(i * 3 + (j - 1)));
					str.append(' ');
				}
			}
			str.append(System.getProperty("line.separator"));
		}
		return str.toString();
	}

	// Add one chess to the chessboard
	public void addOneChess(Chess chess) {
		// Add some code here
		// int index=chess.getX()+chess.getY()*3;
		// for(int i=0;i<locatedPositionsBF.length();i++){
		// if(index==(Integer.parseInt(locatedPositionsBF.charAt(i)+""))){
		// io.writeToConsole("sorry not allowed\nplease input again\n");
		// this.addOneChess(new Chess(chess.getPlayer(), io.readFromConsole()));
		// }
		// }
		// this.chessList.remove(index);
		// this.chessList.add(index,chess);
		// locatedPositionsBF.append(index);
		// return;
		while (true) {
			int index = chess.getX() + chess.getY() * 3;
			boolean isLocated = false;
			for (int i = 0; i < locatedPositionsBF.length(); i++) {
				if (index == (Integer.parseInt(locatedPositionsBF.charAt(i)
						+ ""))) {
					isLocated = true;
					break;
				}
			}
			if (!isLocated) {
				this.chessList.remove(index);
				this.chessList.add(index, chess);
				locatedPositionsBF.append(index);
				break;
			}
			else{
				io.writeToConsole("sorry not allowed\nplease input again\n");
				chess=(new Chess(chess.getPlayer(), io.readFromConsole()));
			}
		}
	}

	// Return the result of the game. See Enum Result
	public Result judge() {
		// Add some code here
		boolean oWin = this.judgeWin(Player.O_PLAYER);
		boolean xWin = this.judgeWin(Player.X_PLAYER);
		if (!oWin &&! xWin&&locatedPositionsBF.length()==9) {
			return Result.DRAW;
		}
		if (oWin)
			return Result.O_WIN;
		else if (xWin)
			return Result.X_WIN;
		else
			return Result.GAMING;

	}

	public boolean judgeWin(Player p) {
		boolean returning = false;
		boolean atCenter = false;
		if (this.chessList.get(4).getPlayer() == p) {
			atCenter = true;
		}
		if (atCenter) {
			for (int i = 0; i < 4; i++) {
				if (this.chessList.get(i).getPlayer() == p) {
					if (this.chessList.get(8 - i).getPlayer() == p) {
						returning = true;
						break;
					}
				}
			}

		} else {
			if (((this.chessList.get(0).getPlayer() == p)
					&& (this.chessList.get(1).getPlayer() == p) && (this.chessList
					.get(2).getPlayer() == p))
					|| ((this.chessList.get(0).getPlayer() == p)
							&& (this.chessList.get(3).getPlayer() == p) && (this.chessList
							.get(6).getPlayer() == p))
					|| ((this.chessList.get(6).getPlayer() == p)
							&& (this.chessList.get(7).getPlayer() == p) && (this.chessList
							.get(8).getPlayer() == p))
					|| ((this.chessList.get(2).getPlayer() == p)
							&& (this.chessList.get(5).getPlayer() == p) && (this.chessList
							.get(8).getPlayer() == p))) {
				returning = true;
			}

		}
		return returning;
	}

}
